<?php

namespace app\system\controller;

use app\common\model\AuthRuleModel;
use org\util\TreeUtil;
use think\Request;

class AuthRuleController extends BaseController
{
    private $authRuleModel;
    private $authRuleCache;
    private $utype;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->utype = $this->request->param('utype','system');
        if (empty($this->utype)) {
            $this->error('未指定模块参数');
        }
        $this->authRuleModel = new AuthRuleModel();
        $this->authRuleCache = cache('auth_rule_' . $this->utype);
    }

    public function index()
    {
        $utypeList    = get_dict('101');
        $list         = $this->authRuleModel->where('utype', $this->utype)->order('rule_sort asc')->select();
        $list         = obj2array($list, ['status_text', 'ismenu_text']);
        $authRuleList = TreeUtil::tree($list, 'title', 'rule_id', 'rule_pid');
        $this->assign('utypeList', $utypeList);
        $this->assign('authRuleList', $authRuleList);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            if (!array_key_exists('status', $data)) {
                $data['status'] = 0;
            }
            $result = $this->validate($data, 'AuthRuleValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $this->authRuleModel->addData($data);
            $this->authRuleModel->updateCache($this->utype);
            $this->success('添加成功', url('index', ['utype' => $this->utype]));
        }
        $this->assign('authRuleList', $this->authRuleCache);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            if (!array_key_exists('status', $data)) {
                $data['status'] = 0;
            }
            if (!array_key_exists('ismenu', $data)) {
                $data['ismenu'] = 0;
            }
            $result = $this->validate($data, 'AuthRuleValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $this->authRuleModel->editData($data);
            $this->authRuleModel->updateCache($this->utype);
            $this->success('修改成功', url('index', ['utype' => $this->utype]));
        }
        $ruleId = $this->request->param('rule_id/d');
        $info   = $this->authRuleModel->get($ruleId);
        $this->assign('authRuleList', $this->authRuleCache);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除规则
     */
    public function del()
    {
        $ruleId = $this->request->param('rule_id', 0);
        if ($this->authRuleCache[$ruleId]['is_son'] == true) {
            $this->error('还存在下级规则，不允许删除！');
        }
        $this->authRuleModel->delData($ruleId);
        $this->authRuleModel->updateCache($this->utype);
        $this->success('删除成功！');
    }

    /**
     * 更新排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $ruleId => $order) {
            $ruleId     = intval($ruleId);
            $order      = intval($order);
            $dataList[] = ["rule_id" => $ruleId, "rule_sort" => $order];
        }
        $this->authRuleModel->saveAll($dataList);
        $this->authRuleModel->updateCache($this->utype);
        $this->success('排序成功！');
    }

    /**
     * 更新缓存
     */
    public function updateCache()
    {
        $this->authRuleModel->updateCache($this->utype);
        $this->success('更新缓存成功！');
    }

    /**
     * 字体图标
     */
    public function icon(){
        return $this->fetch();
    }

    public function copyCate()
    {
        $utype=$this->request->param('utype',0);
        if ($this->request->isPost()){
            $data=$this->request->param('tree/a',0);
            foreach ($data as $value){
                $this->addCate($value,$utype,0);
            }
        }
        if ($utype){
            $rule = cache('auth_rule_' . $utype);
            $rule=TreeUtil::channelLevel($rule,0,"&nbsp;",'rule_id','rule_pid');
            return $rule;
        }else{
            return $this->fetch();
        }
    }
    function addCate($arr,$utype,$pid=0){
        $info=$this->authRuleModel->where('rule_id',$arr['id'])->find()->toArray();
        unset($info['rule_id']);
        $info['rule_pid']=$pid;
        $info['utype']=$utype;
        $this->authRuleModel->addData($info);
        "INSERT INTO `auth_rule` (`rule_pid` , `utype` , `title` , `module` , `controller` , `action` , `status` , `ismenu` , `condition` , `icon` , `rule_sort` , `rule_id`) 
                            VALUES (0 , 'member' , '网站设置' , 'system' , '' , '' , 1 , 1 , '' , 'fa-cogs' , 2 , 226)";
        if (!empty($arr['children'])){
            $rule_pid=$this->authRuleModel->rule_id;
            foreach ($arr['children'] as $child){
                $this->addCate($child,$utype,$rule_pid);
            }
        }

    }
}